﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class PayItemManager: BusinessLogic
    {

        public ObjectTable GetCourseTable(decimal domainID)
        {
            return new ObjectTable(typeof(PAY_TERMS), "this.DOMAINID='" + domainID + "'");
        }

        public PAY_TERMS Load(decimal id)
        {
            return (PAY_TERMS)ObjectRow.Load(typeof(PAY_TERMS), string.Format("this.ID='{0}'", id));
        }

        public PAY_TERMS LoadWithDomainID(string SAPCODE, decimal DomainID,decimal ID)
        {
            return (PAY_TERMS)ObjectRow.Load(typeof(PAY_TERMS), string.Format("this.SAPCODE='{0}' AND this.DOMAINID='{1}' AND this.ID<>'{2}'", SAPCODE, DomainID,ID));
        }

        public PAY_TERMS GetOnePayitem(decimal id)
        {
            DataSet ds = Jumpton.Data.SqlData.OpenSP("sp_GetOnePayItemById", id);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                PAY_TERMS payTerms = new PAY_TERMS();
                DataRowView drv = ds.Tables[0].DefaultView[0];
                payTerms.SAPCODE = drv["SAP_CODE"].ToString() ;
                payTerms.DESCRIPTION = drv["DESCRIPTION"].ToString()  ;
                payTerms.TYPE = int.Parse(drv["TYPE"].ToString()) ;
                payTerms.PERCENT =decimal.Parse( drv["PERCENT"].ToString())  ;
                payTerms.DUEDAYS =decimal.Parse( drv["DUE_DAYS"].ToString())  ;
                payTerms.ACTIVE = drv["ACTIVE"].ToString() == "True"  ;
                payTerms.ID = decimal.Parse(drv[0].ToString());

                return payTerms;
            }
            return null;
        }

        public static DataSet GetAllPayitems(decimal domainID)
        {
            return Jumpton.Data.SqlData.OpenSP("sp_GetAllPayItems", domainID);
        }

        public decimal Save(PAY_TERMS term)
        {
            if (term != null)
            {
                try
                {
                    if (LoadWithDomainID(term.SAPCODE, term.DOMAINID,term.ID) != null)
                    {
                        throw new Exception("代码已存在");
                    }
                   
                    term.Save();
                }
                catch (Exception err)
                {
                    message = err.Message;
                    throw err;
                }
            }
            return term.ID;
        }

       

    }
}
